import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
// 兼容低版本浏览器插件
import legacyPlugin from "@vitejs/plugin-legacy";
// 解决vue动态引入路由报警告错误
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
// 解决vue3.0在setUp模式下TS环境中添加组件名称
import VewSetupExtend from "vite-plugin-vue-setup-extend"
// 解决vue3.0里面支持JSX语法
import vueJsx from "@vitejs/plugin-vue-jsx"

// @ts-ignore
import path from 'path';
// @ts-ignore
import Components from 'unplugin-vue-components/vite';
// @ts-ignore
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers';
// @ts-ignore 引入插件处理gzip;
import viteCompression from 'vite-plugin-compression';

export default defineConfig(({command, mode}) => {
    let config = {
        base: "./",
        plugins: [
            viteCompression(),
            vue(),
            Components({
                dts: true,
                resolvers: [ElementPlusResolver()],
            }),
            dynamicImportVars({
                include: ["./src/router/index.js"]
            }),
            VewSetupExtend(),
            vueJsx(),
        ],
        server: {
            port: 9999,
            fs: {
                strict: false
            },
            proxy:{
                '/api/': {
                    target: 'http://172.23.244.169:18200',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, '')
                },
            },
        },
        resolve: {
            alias: {
                // @ts-ignore
                "src": path.join(__dirname, "src"),
                // @ts-ignore
                "api": path.join(__dirname, "src/assets/js/Api.js"),
                // @ts-ignore
                "ue4Api": path.join(__dirname, "src/assets/UE4/index.js"),
                // @ts-ignore
                "tool": path.join(__dirname, "src/assets/js/tool.js"),
                // @ts-ignore
                "ecApi": path.join(__dirname, "src/assets/js/echartsConfig.js")
            }
        },
        build: {
            commonjsOptions: {
                output: {
                    generatedCode: {
                        type: "es5"
                    }
                }
            },
            chunkSizeWarningLimit: 1500,
            minify: "terser",
            terserOptions: {
                compress: {
                    drop_console: false,
                    drop_debugger: false,
                }
            },
            optimizedeps: {
                esbuildoptions: {
                    target: 'es2020'
                }
            },
            assetsInlineLimit: 40960
        }
    };
    // 如果现在是打包的命令并且是预发版状态需要清除日志和debugger;
    if (command == "build" && (mode == "pre-release" || mode == "release")) {
        config.build.terserOptions.compress.drop_console = true;
        config.build.terserOptions.compress.drop_debugger = true;
    }
    return config;
})
